package com.jsits.v2x.common.dataClient;

import com.jsits.framework.core.web.ActionResult;
import com.jsits.v2x.common.dto.ControlDto;
import com.jsits.v2x.common.enums.NumberColor;
import com.jsits.v2x.common.enums.DevicePosition;
import com.jsits.v2x.common.enums.TextContent;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.*;

/**
 * 屏幕或设备控制
 */
public class DataClient {
    // http客户端
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();

    public List<ControlDto> GetControlStatus(String url) {
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);//执行请求
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {//请求成功执行
                HttpEntity entity = response.getEntity();//获取返回的数据
                String s = EntityUtils.toString(entity);//转换成字符串
                //DataResualt actionResult = JSONObject.parseObject(s, DataResualt.class);//转换成JSON格式
                //ActionResult actionResult = JSONObject.parseObject(s, ActionResult.class);//转换成JSON格式
                ActionResult actionResult = ActionResult.fromJson(s);
                if (actionResult.getCode() == 0) {//返回的状态
                    ArrayList<LinkedHashMap<String, Object>> dataList = (ArrayList<LinkedHashMap<String, Object>>)actionResult.getData();
                    ArrayList<ControlDto> controlDtos = new ArrayList<>();
                    for (LinkedHashMap<String, Object> data:dataList) {
                        ControlDto controlDto = new ControlDto();
                        String devicePosition = (String)data.get("devicePosition");
                        if(devicePosition != null) {
                            if(devicePosition.equals("DEVICE_POSITION_MAIN_1")) {
                                controlDto.setDevicePosition(DevicePosition.DEVICE_POSITION_MAIN_1);
                            } else if(devicePosition.equals("DEVICE_POSITION_MAIN_2")) {
                                controlDto.setDevicePosition(DevicePosition.DEVICE_POSITION_MAIN_2);
                            } else if(devicePosition.equals("DEVICE_POSITION_SUB_1")) {
                                controlDto.setDevicePosition(DevicePosition.DEVICE_POSITION_SUB_1);
                            } else if(devicePosition.equals("DEVICE_POSITION_SUB_2")) {
                                controlDto.setDevicePosition(DevicePosition.DEVICE_POSITION_SUB_2);
                            }
                        }
                        String textContent = (String)data.get("textContent");
                        if(textContent != null) {
                            if(textContent.equals("TEXT_CONTENT_MAIN_UNDER_NOMAN")) {
                                controlDto.setTextContent(TextContent.TEXT_CONTENT_MAIN_UNDER_NOMAN);
                            } else if(textContent.equals("TEXT_CONTENT_MAIN_UNDER_MAN")) {
                                controlDto.setTextContent(TextContent.TEXT_CONTENT_MAIN_UNDER_MAN);
                            } else if(textContent.equals("TEXT_CONTENT_MAIN_OVER_NOMAN")) {
                                controlDto.setTextContent(TextContent.TEXT_CONTENT_MAIN_OVER_NOMAN);
                            } else if(textContent.equals("TEXT_CONTENT_MAIN_OVER_MAN")) {
                                controlDto.setTextContent(TextContent.TEXT_CONTENT_MAIN_OVER_MAN);
                            }
                        }
                        Integer numberContent = (Integer)data.get("numberContent");
                        if(numberContent != null) {
                            controlDto.setNumberContent(numberContent);
                        }
                        String numberColor = (String)data.get("numberColor");
                        if(numberColor != null) {
                            if(numberColor.equals("GREEN")) {
                                controlDto.setNumberColor(NumberColor.GREEN);
                            } else if(numberColor.equals("RED")) {
                                controlDto.setNumberColor(NumberColor.RED);
                            }
                        }
                        controlDtos.add(controlDto);
                    }
                    return  controlDtos;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
